import { Editor, Element } from 'slate';
import { CustomElement } from '@/plugins/types';

/***
 * @Help 获取文档当前行的节点
 */
export const getBlockMatch = (editor: Editor) => {
  const match = Editor.above<CustomElement>(editor, {
    match: (n) => {
      return Element.isElement(n) && Editor.isBlock(editor, n);
    },
  });

  if (!match?.length) throw new Error('getBlockMatch Error');
  const [block, path] = match;
  return { block, path };
};

/**
 * @Help 传入一个对象,返回对象中key对应的值为true的key
 * @param config
 * @returns
 */
export const getMatchKeyOfObject = (config: Record<string, boolean>) => {
  for (const item in config) {
    if (item) {
      return item;
    }
  }
  return null;
};
